import 'package:flutter/material.dart';

/// 页面跳转
class IntentForResultApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.blue),
      home: HomeApp(),
      routes: <String, WidgetBuilder>{
        '/a': (BuildContext context) => MyPage(
              title: 'Page A',
            ),
        '/b': (BuildContext context) => MyPage(
              title: 'Page B',
            ),
        '/c': (BuildContext context) => MyPage(
              title: 'Page C',
            ),
      },
    );
  }
}

/// 主页
class HomeApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HomeAppState();
}

class HomeAppState extends State<HomeApp> {
  /// 第一种异步方式 async await
  /// 启动并等待返回结果
  _navigator(BuildContext context) async {
    var result = await Navigator.push(context,
        new MaterialPageRoute(builder: (context) => new MyPage(title: '异步方式')));
    print("$result");
  }

  /// 第二种
  /// 跳转页面，必须在MaterialApp的routes中注册
//  _navigator(BuildContext context) {
//    // 类似于StartActivityForResult
//    Navigator.of(context).pushNamed('/b').then((result) {
//      print(result);
//    });
//  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('主页'),
        ),
        body: Center(
          child: MaterialButton(
            onPressed: () {
              _navigator(context);
            },
            child: Text('跳转'),
          ),
        ),
      );
}

class MyPage extends StatelessWidget {
  final String title;

  MyPage({Key key, this.title}) : super(key: key);

  _navigator(context) {
    Navigator.of(context).pop({"lat": 43.821757, "long": -79.226392});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
      ),
      body: Center(
        child: MaterialButton(
          onPressed: () {
            _navigator(context);
          },
          child: Text('关闭'),
        ),
      ),
    );
  }
}
